//
//  RewardView.m
//  ImGuider
//
//  Created by 王鹏宇 on 2018/1/15.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "RewardView.h"
#import "RewardCell.h"


@interface RewardView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView * collectionView;

@property (nonatomic, strong)UILabel * rewardCountLabel;

@property (nonatomic, strong)UILabel * currentBtcoinLabel;

@property (nonatomic, strong)UIButton * rewardBtn;

@property (nonatomic, strong)UIButton * rechargeBtn;

@property (nonatomic, strong)UIView * headIconView;

@property (nonatomic) NSInteger selectIndex;

@property (nonatomic, strong) RewardMsgModel *model;


@end



@implementation RewardView


- (instancetype)init {
    
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}


- (void)setup {
    
    self.backgroundColor = [UIColor whiteColor];
    _headIconView = [[UIView alloc] initWithFrame:CGRectMake(0, 16, kScreenWidth - 32, 40)];
    
    [_headIconView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showRewardRecord)]];
    [self addSubview:_headIconView];
    
    _rewardCountLabel = [UILabel new];
    _rewardCountLabel.font = Font(12);
    _rewardCountLabel.textColor = kSubitleColor;
    [_rewardCountLabel sizeToFit];
    [self addSubview:_rewardCountLabel];
    [_rewardCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_headIconView.mas_bottom).offset(5);
        make.centerX.equalTo(_headIconView.mas_centerX);
    }];

    //    _rewardCountLabel.text = @"134次打赏";
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(Width,Height);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"RewardCell" bundle:nil] forCellWithReuseIdentifier:@"RewardCell"];
    [self addSubview:_collectionView];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_rewardCountLabel.mas_bottom).offset(16);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(Height + 10);
    }];
    
    _rewardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_rewardBtn];
    
    [_rewardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo (_collectionView.mas_bottom).offset (16);
        make.left.equalTo(self.mas_left).offset(16);
        make.right.equalTo(self.mas_right).offset(-16);
        make.height.mas_equalTo(50);
    }];
    
    [_rewardBtn setTitle:MYLocalizedString(@"COIN_REWARD", nil) forState:UIControlStateNormal];
    [_rewardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_rewardBtn setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forState:UIControlStateNormal];
    _rewardBtn.layer.cornerRadius = 5;
    _rewardBtn.clipsToBounds = YES;
    [_rewardBtn addTarget:self action:@selector(goReward) forControlEvents:UIControlEventTouchUpInside];

    
    _currentBtcoinLabel = [UILabel new];
    _currentBtcoinLabel.font = Font(12);
    _currentBtcoinLabel.textColor = kContentColor;
    [_currentBtcoinLabel sizeToFit];
    [self addSubview:_currentBtcoinLabel];
    
    [_currentBtcoinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_rewardBtn.mas_bottom).offset(16);
        make.centerX.equalTo(_headIconView.mas_centerX);
    }];
    
    _rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _rechargeBtn.frame = CGRectMake(rechargeBtnX, currentBitcoinY - 6, 40, 18);
    [_rechargeBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
    [_rechargeBtn setTitle:MYLocalizedString(@"COIN_TOPUP", nil) forState:UIControlStateNormal];
    _rechargeBtn.layer.cornerRadius = 9.0f;
    
    _rechargeBtn.layer.borderWidth = 1.0f;
    _rechargeBtn.layer.borderColor = kThemeColor.CGColor;
    
     _rechargeBtn.titleLabel.font = Font(12);
    
    [_rechargeBtn addTarget:self action:@selector(goRecharge) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self addSubview:_rechargeBtn];
    
    [_rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(_currentBtcoinLabel.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-16);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(18);
    }];
}

- (void)reloadData {
    
    self.currentBtcoinLabel.text = [NSString stringWithFormat:@"%@:%@%@",MYLocalizedString(@"COIN_BALANCE", nil),self.model.remain,MYLocalizedString(@"PUBLIC_COIN_NAME", nil)];
    
    self.rewardCountLabel.text = [NSString stringWithFormat:@"%@%@",self.model.rewardcount,MYLocalizedString(@"COIN_REWARD_COUNT", nil)];
    
    [self.collectionView reloadData];
    
    [_headIconView removeAllSubviews];
    
    CGFloat imageX = 0;
    
    for (NSString *imageStr in self.model.headimgArray) {
        
        if (imageStr.length == 0) {
            continue;
        }
        
        UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, 5, 30, 30)];
        [imageview setImageURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"default"]];
        imageview.layer.cornerRadius = 15;
        imageview.clipsToBounds = YES;
        [_headIconView addSubview:imageview];
        
        imageX += 18;
    }
    
    [_headIconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_top).offset(16);
        make.centerX.equalTo(self.mas_centerX);
        make.width.mas_equalTo(imageX + 12);
        make.height.mas_equalTo(40);
        
    }];
}


- (void)setLineid:(NSString *)lineid {
    
    _lineid = lineid;
    
    if (!lineid) {
        return;
    }
    
    [ActivityIndicatorView showHUDAddedTo:self];
    @weakify(self);
    [NetAPIManager getRewardMessageWithLineid:lineid block:^(RewardMsgModel *model, NSError *error) {
        @strongify(self);
        [ActivityIndicatorView hideHUDAddedTo:self];
        self.model = model;
        [self reloadData];
    }];
}


- (void)goReward{
    
    if (self.model.levels.count <= self.selectIndex) {
        return;
    }
    
    RewardModel *model = self.model.levels[self.selectIndex];
    
    if ([_delegate respondsToSelector:@selector(rewardWithModel:)]) {
        [_delegate rewardWithModel:model];
    }
}

- (void)goRecharge {
    
    if (_delegate && [_delegate respondsToSelector:@selector(recharge)]) {
        
        [_delegate recharge];
    }
}

- (void)showRewardRecord {
    
    if ([_delegate respondsToSelector:@selector(showRewardRecord)]) {
        [_delegate showRewardRecord];
    }
}

#pragma mark    -UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    return self.model.levels.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    RewardModel *model = self.model.levels[indexPath.row];
    
    
    RewardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RewardCell" forIndexPath:indexPath];
    
    cell.level = model;
    
    if (self.selectIndex == indexPath.row) {
        cell.isSelect = YES;
    } else {
        
        cell.isSelect = NO;
    }
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    self.selectIndex = indexPath.row;
    
    [self.collectionView reloadData];
    
    
}



@end

